package com.zl.customer.web.util;/**
 * @author suke
 * @create 2021-08-02 10:43
 */

import com.zl.customer.service.UserinfoService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

/**
 * TODO
 * @ClassName: SpringUtils
 * @author: suke
 * @since: 2021/8/2 10:43
 */
public class SpringUtils {

    public static <T> T getServiceBean(Class<T> clazz, ServletContext application){
        //手动从Application域获取
        //ApplicationContext applicationContext = (ApplicationContext)application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        //使用Spring-web提供工具类: WebApplicationContextUtils
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(application);
        return  applicationContext.getBean(clazz);
    }
}
